package com.wanrue.tcc.context;

import com.wanrue.tcc.commom.Invoker;
import com.wanrue.tcc.enums.Status;
import lombok.Data;

import java.io.Serializable;

/**
 * 事务参与者
 * Created by changmingxie on 10/27/15.
 */
@Data
public class Participant implements Serializable {

    private static final long serialVersionUID = 4127729421281425247L;

    /**
     * 事务id 事务编号
     */
    private Xid xid;

    /**
     * 参与者confirm方法
     */
    private Invocation confirm;

    /**
     * 参与者cancel方法
     */
    private Invocation cancel;

    public Participant(Xid xid, Invocation confirm, Invocation cancel) {
        this.xid = xid;
        this.confirm = confirm;
        this.cancel = cancel;
    }

    /**
     * 回滚 cancel
     */
    public void rollback() {
        Invoker.invoke(new Context(xid, Status.CANCELLING), cancel);
    }

    /**
     * 提交 confirm
     */
    public void commit() {
        Invoker.invoke(new Context(xid, Status.CONFIRMING), confirm);
    }
}
